<template>
  <div id="allmap">
    <div id="map"></div>


    <!-- 图例 -->
    <div class="person_legend" v-if="showLegend">
      <h3>
        图例
        <i class="iconfont icon-shanchu" @click="showLegend = false"></i>
      </h3>
      <p>
        <span>
          <img :src="legend.toiletDMDX" alt />
        </span>
        <span style="font-size: 12px">地面地下式公厕</span>
      </p>
      <p>
        <span>
          <img :src="legend.toiletDL" alt />
        </span>
        <span style="font-size: 12px">独立式公厕</span>
      </p>
      <p>
        <span>
          <img :src="legend.toiletDL" alt />
        </span>
        <span style="font-size: 12px">移动式公厕</span>
      </p>
      <p>
        <span>
          <img :src="legend.toiletFJ" alt />
        </span>
        <span style="font-size: 12px">附建式公厕</span>
      </p>
    </div>
    <el-button
      style="position: absolute; right: 0px; top: 25px"
      size="mini"
      type="primary"
      @click="onlyShowHeader()"
      icon="el-icon-circle-close"
        >隐藏其它</el-button
      >
      <el-button
        style="position: absolute; right: 122px; top: 25px"
        size="mini"
        type="primary"
        @click="requestFullScreen()"
        v-if="!isFull"
      icon="el-icon-full-screen"
        >全屏</el-button
      >
      <el-button
        style="position: absolute; right: 122px; top: 25px"
        size="mini"
        type="primary"
        @click="exitFullscreen()"
        v-else
        icon="el-icon-full-screen"
        >退出全屏</el-button
      >
    <!-- 右上方显示隐藏图标操作栏,多选 -->
    <div class="map-operation">
      <ul>
        <li
          v-for="(item,index) in mapOperationsList"
          :class="{'active': item.active}"
          :key="index"
          @click="changeOperationValue(item, index)"
        >
          <i :class="item.icon"></i>
          {{ item.name }}
        </li>
      </ul>
    </div>
   
    <!-- 左下角 列表展开按钮-->
    <div class="mini_legend" v-if="hideFooter" @click="showFooter()">
      实时监控列表
    </div>
    <!-- 右下角 图例展开按钮-->
    <div
      class="mini_legend right"
      v-if="!showLegend"
      @click="showLegend = !showLegend"
    >
      图例
    </div>
  </div>
</template>

<script>
import imgCar from '@/assets/img/MapMarker/rubbishCar/xifen.png'
import imgCar0 from '@/assets/img/MapMarker/rubbishCar/xifenUnline.png'
import imgToiletDMDX from '@/assets/img/MapMarker/wcToilet/dimiandixia.png'
import imgToiletDL from '@/assets/img/MapMarker/wcToilet/duli.png'
import imgToiletFJ from '@/assets/img/MapMarker/wcToilet/fujian.png'
import imgToiletYD from '@/assets/img/MapMarker/wcToilet/yidong.png'
import imgWorkerLD from '@/assets/img/MapMarker/wcWorker/liudong.png'
import imgWorkerZC from '@/assets/img/MapMarker/wcWorker/zhuchang.png'

import imgWorkerLD0 from '@/assets/img/MapMarker/wcWorker/liudongUnline.png'
import imgWorkerZC0 from '@/assets/img/MapMarker/wcWorker/zhuchangUnline.png'

import warningMark from '@/assets/img/warning-mark.png'
import eventBus from '@/eventBus.js'
import MapStr from '@/main.js'
import Schart from 'vue-schart';
import { newMap } from '@/common/js/map.js'
export default {
  components: {
    Schart
  },
  data () {
    return {
      MouseEvent: {
        top: 0,
        left: 0,
        isShow: false
      },
      isFull: false,
      map: null,
      mapZoom: 16,
      // 班组区域
      groupPolygon: null,
      cleanerPolygon: null,
      status: {},
      legend: {
        car: imgCar,
        toiletDMDX: imgToiletDMDX,
        toiletDL: imgToiletDL,
        toiletFJ: imgToiletFJ,
        toiletYD: imgToiletYD,
        workerLD: imgWorkerLD,
        workerZC: imgWorkerZC
      },
      MapV: {
        dataSetDic: {}
      },
      MapOverlay: {
        arrayCLCMapOverlay: [],
        arrayToiletMapOverlay: []
      },
      Data: {
        arrayForType: [],
        arrayToiletData: [],
      },
      // warningNum: 0,
      // 筛选数据
      hideFooter: false,
      showLegend: true,

      mapInfoTabs: [{
        icon: 'el-icon-truck'
      }, {
        icon: 'el-icon-user'
      }, {
        icon: 'el-icon-collection-tag'
      }],
      mapInfoTabValue: 0,
      showMapInfo: true,


      mapOperationsList: [ {
        name: '公厕',
        icon: 'el-icon-toilet-paper',
        active: true
      }],
      realtimeStatistics: {
        wcWorkerOnline: '',
        wcWorkerAll: '',
        wcCarOnline: '',
        wcCarAll: '',
      },
      checkbox_large: false,
      checkbox_small: false,
      checkbox_village: false,
      checkbox_carLabel: true,
    }
  },
  destroyed () {
    eventBus.$off('mapData_ToiletCar')
    eventBus.$off('mapData_ToiletWorker')
    eventBus.$off('mapData_Toilet')
    eventBus.$off('mapDBrowToilet')
    eventBus.$off('mapDBrowToiletCar')
    eventBus.$off('mapDBrowToiletWorker')
    eventBus.$off('realtimeOnlineStatistics_WC')
  },
  created () {
    // 自动刷新 + 显示人员标记点

    eventBus.$on('mapData_Toilet', val => {
      this.Data.arrayToiletData = val
      this.Data.arrayForType = val
      this.mapOperationsList[0].num = val.length
      this.drawMapV()
    })
    // 双击人员列表某一行，显示标记点，信息窗体
    eventBus.$on('mapDBrowToilet', val => {
      this.dbClickToilet(val)
    })
    // 隐藏底部列表
    eventBus.$on('toHideFooter', () => {
      this.hideFooter = true;
    })
    // 隐藏左侧面板
    eventBus.$on('toHideMapInfo', () => {
      this.showMapInfo = false;
      this.mapInfoTabValue = '';
    })
    // 隐藏图例
    eventBus.$on('toHideLegend', () => {
      this.showLegend = false;
    })
  },
  mounted () {
    // 显示地图
    this.mapShow()
  },
  methods: {
    // 显示地图
    async mapShow () {
      // 百度地图API功能
      this.map = newMap();
      // 初始化地图,设置中心点坐标和地图级别
      this.map.centerAndZoom(new BMap.Point(MapStr.DefaultPointX, MapStr.DefaultPointY), this.mapZoom)
    },
    // 显示标记点
    drawMapV () {
      // 销毁旧图层
      // this.MapV.arrayVIcon.forEach(element => {
      //   element.destroy();
      //   element = {}
      // });
      // this.MapV.arrayVText.forEach(element => {
      //   element.destroy();
      //   element = {}
      // });
      // this.MapV.arrayVIcon = []
      // this.MapV.arrayVText = []
      for (var key in this.MapV.dataSetDic) {
        this.MapV.dataSetDic[key].set([]);
      }

      //如果关闭车辆显示，结束执行
      if (!this.mapOperationsList[0].active) {
        return
      }
      //因为需要加载不同的图片，所以将车辆数据按车辆类型、是否在线分成不同数组this.Data.arrayForType
      this.Data.arrayForType.forEach(arrayDataForSameIcon => {
        //如果数据中无数据，退出循环
        if (arrayDataForSameIcon.length == 0) {
          return;
        }

        const _self = this;

        let img = new Image();
        img.src = require('@/assets/img/MapMarker/wcToilet/' + arrayDataForSameIcon[0].img + '.png')
        img.onload = function () {
          //循环加载同一张图片的车辆数据
          let arrayImage = arrayDataForSameIcon.map(v => {
            v.geometry = {
              type: 'Point',
              coordinates: [v.LONGITUDE, v.LATITUDE]
            }
            v.icon = img;
            v.text = v.NAME;
            return v;
          })

          let key = arrayDataForSameIcon[0].TYPE

          if (_self.MapV.dataSetDic.hasOwnProperty(key)) {

            _self.MapV.dataSetDic[key].set(arrayImage);
            // _self.MapV.iconLayerDic[key].show()
            // _self.MapV.textLayerDic[key].show()

          } else {

            let optionsICON = {
              draw: 'icon',
              width: 35, // 规定图像的宽度
              height: 35, // 规定图像的高度
              // offset: { // 文本便宜值
              //   x: 0,
              //   y: 20
              // }
            }

            let optionsTEXT = {
              draw: 'text',
              size: 14,
              avoid: true, // 开启文本标注避让
              textBaseline: 'middle',
              offset: { // 文本便宜值
                x: 0,
                y: 30
              }
            }

            let dataSet = new mapv.DataSet(arrayImage);

            _self.MapV.dataSetDic[key] = dataSet;
            new mapv.baiduMapLayer(_self.map, dataSet, optionsICON)
            new mapv.baiduMapLayer(_self.map, dataSet, optionsTEXT)
          }
        }
      });
    },
    // async handleCheckToilet1 () {
    //   if (this.mapOperationsList[0].active) {
    //     const _self = this;
    //     this.Data.arrayToiletData.forEach(item => {
    //       var img = imgToiletDMDX
    //       switch (item.TYPE) {
    //         case '独立式公厕':
    //           img = imgToiletDL
    //           break;
    //         case '附建式公厕':
    //           img = imgToiletFJ
    //           break;
    //         case '移动式公厕':
    //           img = imgToiletYD
    //           break;
    //       }
    //       var marker = this.createMarker(item.LONGITUDE, item.LATITUDE, img)
    //       marker.disableMassClear()
    //       this.MapOverlay.arrayToiletMapOverlay.push(marker)
    //       var label = new BMap.Label(item.NAME, { offset: new BMap.Size(0, 52) })
    //       marker.setLabel(label)
    //       marker.setTitle(item.ID)

    //       // 信息窗体
    //       const opts = {
    //         width: 200,     // 信息窗口宽度
    //         height: 110,     // 信息窗口高度
    //         title: '公厕信息', // 信息窗口标题
    //       }
    //       const str = `<p style="margin-bottom: 5px;">名称：${item.NAME}</p>
    //       <p style="margin-bottom: 5px;">等级：${item.LEVEL}</p>
    //                 <p style="margin-bottom: 5px;">类型：${item.TYPE}</p>
    //                 <p style="margin-bottom: 5px;">位置：${item.LOCATION}</p>
    //                 `
    //       const infoWindow = new BMap.InfoWindow(str, opts)  // 创建信息窗口对象 
    //       marker.addEventListener("click", function () {
    //         console.log(this.getTitle())
    //         eventBus.$emit('mapClick_ToiletDetail', this.getTitle())
    //         // this.openInfoWindow(infoWindow);
    //       });
    //     })
    //   } else {
    //     this.MapOverlay.arrayToiletMapOverlay.forEach(item => {
    //       item.enableMassClear()
    //       this.map.removeOverlay(item)
    //     })
    //     this.MapOverlay.arrayToiletMapOverlay = []
    //   }
    // },
    // // 创建标记点
    // createMarker (LONGITUDE, LATITUDE, iconURL) {
    //   const pt = new BMap.Point(LONGITUDE, LATITUDE)
    //   const myIcon = new BMap.Icon(iconURL, new BMap.Size(65, 65))
    //   const marker = new BMap.Marker(pt, { icon: myIcon })// 创建标注
    //   this.map.addOverlay(marker)// 将标注添加到地图中
    //   return marker
    // },
    dbClickToilet (val) {
      // 标记点地图居中
      this.map.centerAndZoom(new BMap.Point(val.LONGITUDE, val.LATITUDE), this.mapZoom)
      // 信息窗体
      const opts = {
        width: 200,     // 信息窗口宽度
        height: 110,     // 信息窗口高度
        title: '公厕信息', // 信息窗口标题
      }
      const str = `<p style="margin-bottom: 5px;">名称：${val.NAME}</p>
                <p style="margin-bottom: 5px;">等级：${val.LEVEL}</p>
                <p style="margin-bottom: 5px;">类型：${val.TYPE}</p>
                <p style="margin-bottom: 5px;">位置：${val.LOCATION}</p>
                `
      const infoWindow = new BMap.InfoWindow(str, opts)  // 创建信息窗口对象 
      const point = new BMap.Point(val.LONGITUDE, val.LATITUDE)//信息窗体坐标
      this.map.openInfoWindow(infoWindow, point) //开启信息窗口
    },
    async handleCheckCLCPolygon () {
      // if (document.getElementById('checkbox_clc').checked) {
      var pointPolygon = []
      this.Data.arrayCLCPolygonData.forEach(item => {
        var point = new BMap.Point(item.经度, item.纬度)
        pointPolygon.push(point)
      })
      // var polygon = new BMap.Polygon([new BMap.Point(117.425438, 38.872788), new BMap.Point(117.426718, 38.873610), new BMap.Point(117.427381, 38.872967), new BMap.Point(117.426111, 38.872155), new BMap.Point(117.425438, 38.872788)], { strokeColor: 'blue', strokeWeight: 2, strokeOpacity: 0.5 })
      var polygon = new BMap.Polygon(pointPolygon)
      polygon.disableMassClear()
      this.map.addOverlay(polygon)
      this.MapOverlay.arrayCLCMapOverlay.push(polygon)
    },
    // 显示底部列表
    showFooter () {
      this.hideFooter = false;
      eventBus.$emit('toShowFooter');
    },
    // 仅显示头
    onlyShowHeader () {
      // this.toHideWarningInfo();
      eventBus.$emit('toHideMapInfo');
      eventBus.$emit('toHideFooter');
      eventBus.$emit('toHideLegend');
    },
    // 进入全屏
    requestFullScreen () {
      this.isFull = true;
      eventBus.$emit('contentToFullScreen');
      var de = document.documentElement;
      if (de.requestFullscreen) {
        de.requestFullscreen();
      } else if (de.mozRequestFullScreen) {
        de.mozRequestFullScreen();
      } else if (de.webkitRequestFullScreen) {
        de.webkitRequestFullScreen();
      }
    },
    // 退出全屏
    exitFullscreen () {
      this.isFull = false;
      eventBus.$emit('contentToDefaultScreen');
      var de = document;
      if (de.exitFullscreen) {
        de.exitFullscreen();
      } else if (de.mozCancelFullScreen) {
        de.mozCancelFullScreen();
      } else if (de.webkitCancelFullScreen) {
        de.webkitCancelFullScreen();
      }
    },
    // 地图信息tab切换
    changeMapInfoTabValue (index) {
      if ((index === this.mapInfoTabValue) && this.showMapInfo) {
        this.showMapInfo = false;
        this.mapInfoTabValue = '';
      } else {
        this.showMapInfo = true;
        this.mapInfoTabValue = index;
      }
    },
    // 改变右上方显示隐藏图标
    changeOperationValue (item, index) {

      item.active = !item.active

      switch (index) {
        case 0:
          this.drawMapV()
          break;
        default:
          break;
      }
    }
  }
}
</script>

<style scoped lang="less">
#allmap {
  position: relative;
  // height: calc(100% - 40px);
  height: 100%;
}
/deep/.BMap_cpyCtrl {
  display: none !important;
}
/deep/.anchorBL {
  display: none !important;
}
#map {
  height: 100%;
}
.person_top {
  width: 150px;
  height: 130px;
  position: absolute;
  left: 10px;
  top: 10px;
  background-color: #b2dfee;
  border: 1px solid #ccc;
  p {
    height: 30px;
    // text-align: center;
    padding-left: 5px;
    padding-left: 10px;
    color: #333;
    span:first-child {
      text-align: center;
      padding-top: 14px;
      display: inline-block;
      width: 30px;
      height: 30px;
      margin-right: 5px;
      vertical-align: middle;
      img {
        // background-size: 30px 30px;
        height: 30px;
      }
    }
  }
}
.person_legend {
  width: 350px;
  position: absolute;
  right: 10px;
  bottom: 10px;
  background-color: rgba(255, 255, 255, 0.9);
  padding: 5px 10px 0 10px;
  border-radius: 5px;
  h3 {
    height: 30px;
    text-align: center;
    line-height: 30px;
    color: #333;
    margin-bottom: 5px;
    i {
      float: right;
      cursor: pointer;
      &:before {
        color: #666;
      }
    }
  }
  p {
    // text-align: center;
    color: #333;
    float: left;
    width: 25%;
    margin-bottom: 20px;
    span {
      display: block;
      text-align: center;
      &:first-child {
        img {
          // background-size: 30px 30px;
          height: 40px;
        }
      }
    }
  }
}
.status_legend {
  position: absolute;
  right: 10px;
  bottom: 10px;
  padding: 0 10px;
  background-color: #f3f3f3;
  border: 1px solid #ccc;
  .public {
    display: inline-block;
    width: 10px;
    height: 10px;
    margin-left: 10px;
    margin-right: 2px;
  }
  .online {
    background-color: #1afa29;
  }
  .outline {
    background-color: blue;
  }
  .warning {
    background-color: red;
  }
}
.mini_legend {
  position: absolute;
  left: 10px;
  bottom: 10px;
  background-color: rgba(255, 255, 255, 0.9);
  border: 1px solid #dcdfe6;
  padding: 5px 10px;
  border-radius: 5px;
  cursor: pointer;
  color: #007acc;
  font-size: 14px;
  &.right {
    right: 10px;
    left: auto;
  }
}
.map-infos-wrap {
  position: absolute;
  left: 10px;
  top: 30px;
  height: 120px;
  .map-infos {
    background: rgba(255, 255, 255, 0.9);
    height: 500px;
    width: 300px;
    border-radius: 5px;
    padding: 10px;
    overflow-x: hidden;
    overflow-y: auto;
    max-height: 100%;
    h3 {
      font-size: 15px;
      line-height: 26px;
      margin-left: 10px;
      i {
        float: right;
        font-style: normal;
        color: #666;
        font-size: 12px;
      }
    }
    .item-wrap {
      display: flex;
      margin-bottom: 20px;
      .item-con {
        flex: 1;
        text-align: center;
        h2 {
          margin-top: 10px;
          line-height: 30px;
          font-size: 20px;
        }
        span {

          font-size: 13px;
          color: #666;
        }
      }
    }
    .icon-wrap {
      margin: 10px 10px 10px 0;
      .icon-con {
        width: 33.33%;
        float: left;
        text-align: center;
        margin: 15px 0;
        position: relative;
        i {
          font-size: 20px;
          display: block;
          margin-bottom: 5px;
          color: #333;
        }
        .el-tag.el-tag--mini {
          position: absolute;
          right: 0;
          top: -8px;
          height: 15px;
          line-height: 15px;
          border-color: #e1dada;
          background: #e1dada;
          color: #666;
        }
        span {
          font-family:STHeiti;
          font-size: 14px;
          color: rgb(34, 34, 34);
        }
        &.hasColor {
          i {
            color: #007acc;
          }
          .el-tag.el-tag--mini {
            font-size: 14px;
            border-color: #007acc;
            background: #007acc;
            color: #fff;
          }
        }
      }
    }
    .map-checkboxs {
      margin-top: 10px;
      p {
        line-height: 30px;
      }
    }
    /deep/.el-progress-bar__outer {
      background: #d8d8d8;
    }
  }
  .info-tabs {
    position: absolute;
    right: -30px;
    top: 20px;
    background: rgba(255, 255, 255, 0.9);
    width: 30px;
    border-radius: 0 3px 3px 0;
    overflow: hidden;
    a {
      display: block;
      padding: 3px 3px 1px 3px;
      text-align: center;
      line-height: 24px;
      cursor: pointer;
      &.active {
        background: #007acc;
        i {
          color: #fff;
        }
      }
    }
  }
  /* 滚动条样式 */
  ::-webkit-scrollbar {
    width: 6px;
    height: 6px;
  }
  ::-webkit-scrollbar-thumb {
    background: #ccc;
    border-radius: 6px;
  }
}
/deep/.el-table.card-table {
  margin-top: 10px;
  background: none;
  thead,
  tbody {
    color: #333;
  }
  tr,
  th {
    background: none !important;
  }
  th div,
  td div {
    font-size: 14px;
  }
  tr:nth-child(2n) {
    background: none;
  }
  tr:hover > td {
    background: none !important;
  }
}
.map-operation {
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  margin-top: 32px;
  top: 10px;
  border-radius: 5px;
  ul {
    li {
      list-style: none;
      float: left;
      color: #ecf0f3;
      background: #b2bbbe;
      font-size: 16px;
      padding: 5px 12px;
      position: relative;
      cursor: pointer;
      border-radius: 2px;
      i {
        margin-right: 3px;
        margin-bottom: 3px;
        &:before {
          font-size: inherit;
          color: inherit;
        }
      }
      &::before {
        display: block;
        content: '';
        width: 1px;
        height: 13px;
        background: #dcdfe6;
        position: absolute;
        right: 0;
        top: 8px;
      }
      &:last-child {
        &::before {
          display: none;
        }
      }
      &.active {
        color: #fff;
        background: #007acc;
        border-radius: 2px;
      }
    }
  }
}
.row_box {
  width: 120px;
  // height: 130px;
  color: #666;
  background-color: #f3f3f3;
  border: 1px solid #ccc;
  position: fixed;
  span {
    display: block;
    text-align: center;
    line-height: 26px;
    cursor: default;
    &:hover {
      color: #fff;
      background-color: #409eff;
    }
  }
}
</style>
